<template>
  <div v-if="showScanBox" class="scanPayBoxWrap">
      <div class="scanPayBox-bg"></div>
      <div class="scanPayBox-main">
          <div class="scanPayBox-close" @click="hideBox">
              <i class="iconfont icon-icon_close"></i>
          </div>
          <div class="scanPayBox-content">
              <div class="qrcode-content" v-if="qrCodeUrl">
                  <img
                      :src="qrCode"
                      v-if="qrCode"
                      :class="{ 'ios-img-touch': !isWeChat }"
                  />
              </div>
              <div class="scanPayhint">请使用微信扫一扫进行支付</div>
              <div class="button-wrap">
                  <span class="btnitem cancel-btn" @click.stop="reloadPage"
                      >购买不成功</span
                  >
                  <span class="btnitem confim-btn" @click.stop="reloadPage"
                      >已完成购买</span
                  >
              </div>
          </div>
      </div>
  </div>
</template>

<script>
import QRCode from 'qrcode';
export default {
  model: {
      prop: 'showScanBox',
      event: 'update'
  },
  props: {
      showScanBox: false,
      qrCodeUrl: {
          type: String,
          default: location.href,
      },
  },
  data() {
      return {
          qrCode: "",
      };
  },
  created() {
      if (this.qrCodeUrl) {
          this.createQrcode(this.qrCodeUrl);
      }
  },
  computed: {
      isWeChat() {
          return window.wx && window.wx.CLIENT_ENV === "wechat";
      },
  },
  methods: {
      // 生成二维码
      createQrcode(qrUrl) {
          QRCode.toDataURL(qrUrl)
              .then((url) => {
                  this.qrCode = url;
              })
              .catch((err) => {
                  console.error(err);
              });
      },
      hideBox() {
          this.$emit("update", false);
      },
      reloadPage() {
          window.location && window.location.reload();
      },
  },
};
</script>

<style lang="scss" scoped>
.scanPayBoxWrap {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 21;
  font-size: 0.22rem;
  overflow: hidden;
  .scanPayBox-bg {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.59);
  }
  .scanPayBox-main {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 5.88rem;
      height: 5.86rem;
      border-radius: 0.06rem;
      background-color: #fff;
      max-width: 480px;
      padding: 0.2rem;
  }
  .scanPayBox-close {
      width: 100%;
      text-align: right;
      margin-bottom: 0.34rem;
      color: #999;
  }
  .scanPayBox-content {
      text-align: center;
  }
  .qrcode-content {
      text-align: center;
      img {
          width: 3rem;
          height: 3rem;
          border-radius: 0.08rem;
      }
  }
  .ios-img-touch {
      -webkit-touch-callout: initial;
  }
  .button-wrap {
      width: 100%;
      padding: 0 0.48rem;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      margin-top: 0.75rem;
      .btnitem {
          display: inline-block;
          width: 1.47rem;
          height: 0.56rem;
          line-height: 0.56rem;
          user-select: none;
          background-color: transparent;
          font-size: 0.22rem;
          outline: none;
          cursor: pointer;
          border: 1px solid rgba(238, 238, 238, 1);
          border-radius: 0.03rem;
      }
      .cancel-btn {
          color: #666666;
          background-color: #fff;
          margin-right: 0.31rem;
      }
      .confim-btn {
          color: #fff;
          background-color: #2a75ed;
          border: 0;
      }
  }
}
</style>
